//
//  RootVCOfHomePage.m
//  ProjectA_ManualGuest
//
//  Created by dingcong on 16/6/14.
//  Copyright © 2016年 丁聪. All rights reserved.
//

#import "RootVCOfHomePage.h"
#import "Masonry.h"
#import "FlowLayoutOfHomePage.h"
#import "SelectionViewCotroller.h"
#import "AttentionViewController.h"
#import "ExpertViewController.h"
#import "ActivityViewController.h"
#import "PrefixHeader.pch"
#import "CellOfHomePage.h"
#import "RootVCOfVidio.h"
#import "VCOfMy.h"
#import "VCOfSearch.h"
#import "Settings.h"
#define tittle_view_W self.titleView.frame.size.width
#define tittle_view_H self.titleView.frame.size.height
@interface RootVCOfHomePage ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, retain)UIView *viewOfRedLine;
@property (nonatomic, retain)UICollectionView *collection;
@property (nonatomic, retain)UIView *titleView;
@property (nonatomic, retain)NSArray *arrOfVC;
@end

@implementation RootVCOfHomePage


- (void)dealloc{
    [super dealloc];
    [_viewOfRedLine release];
    [_collection release];
    [_titleView release];
    [_arrOfVC release];
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    [self config];
 
    [self creatColletionView];
    [self collectionconfig];
    
    
    
    
}

- (void)viewDidAppear:(BOOL)animated{
    
    self.navigationController.navigationBar.hidden = NO;
    
    
}

#pragma mark - 设置navigationItem tabbarItem控件
- (void)config{
    
    
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"首页" image:[UIImage imageNamed:@"1.png"] tag:100];

    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.0 / 256.0f  green:208.0 / 256.0f blue:252.0 / 256.0f alpha:1];
    
    
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"11.png"] style:UIBarButtonItemStylePlain target:self action:@selector(handleSearch:)];
    
    
    [self.navigationItem setTitle:@"首页"];
    
    
    NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName,nil];
    [self.navigationController.navigationBar setTitleTextAttributes:attributes];
    
    
    
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];

    
    [self.view addSubview:self.titleView];
    
    
    [self.navigationItem.rightBarButtonItem release];
    
    
}

#pragma mark - navigationitem点击事件
- (void)handleSearch:(UIBarButtonItem *)search
{
    VCOfSearch *vc = [[VCOfSearch alloc]init];
    
    
    
    [self.navigationController pushViewController:vc animated:YES];
    
    [vc release];
    
    
    
    
}

- (void)handleWrite:(UIBarButtonItem *)write{
    
    
    
    
    
    
}

#pragma mark - 创建CollectionView
- (void)creatColletionView{
    FlowLayoutOfHomePage *layout = [[FlowLayoutOfHomePage alloc]init];
    self.collection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 100, SCREEN_WIDTH, SCREEN_HEIGHT - 100 - 49) collectionViewLayout:layout];
    
    
    [self config];
    layout.itemSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT);
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.collection.backgroundColor = [UIColor lightGrayColor];
    self.collection.pagingEnabled = YES;
    self.collection.showsHorizontalScrollIndicator = NO;
    self.collection.dataSource = self;
    [self.collection addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:nil];
    self.collection.backgroundColor = [UIColor whiteColor];
    
    [self.collection registerClass:[CellOfHomePage class] forCellWithReuseIdentifier:@"pool"];
    [self.view addSubview:self.collection];
    
    
    
    [_collection release];
    [layout release];
}


#pragma mark - KVO
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
    
    CGFloat x = [[change objectForKey:@"new"]CGPointValue].x / 4.0f + 25;
    
    self.viewOfRedLine.frame = CGRectMake(x, 64 + 34.5, CGRectGetWidth(self.view.bounds)  / 6.7 - 10, 1);
    
    
}


#pragma mark - CollectionView Datasource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    self.arrOfVC = self.childViewControllers;
    return self.arrOfVC.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    CellOfHomePage *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool" forIndexPath:indexPath];
    
    
    return cell;
    
}

#pragma mark - CollectionView嵌套TableView
- (void)collectionconfig{
    
    
    
    
    SelectionViewCotroller *selection = [[SelectionViewCotroller alloc]init];
    selection.tableView.backgroundColor = [UIColor clearColor];
    AttentionViewController *attention = [[AttentionViewController alloc]init];
    attention.tableView.backgroundColor = [UIColor clearColor];
    ExpertViewController *expert = [[ExpertViewController alloc]init];
    expert.tableView.backgroundColor = [UIColor clearColor];
    ActivityViewController *acticity = [[ActivityViewController alloc]init];
    acticity.tableView.backgroundColor = [UIColor clearColor];
#pragma mark - 接收通知  改变颜色
    [[NSNotificationCenter defaultCenter]addObserverForName:@"changeBackgroundColor" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
        
        self.collection.backgroundColor = [[Settings shareSettings]color:WhiteToGray];
        self.tabBarController.tabBar.barTintColor = [[Settings shareSettings]color:WhiteToGray];
        self.navigationController.navigationBar.barTintColor = [[Settings shareSettings]color:SkyBlurToGray];
        self.view.backgroundColor = [[Settings shareSettings]color:WhiteToGray];
    }];
    [self addChildViewController:selection];
    [self.collection addSubview:selection.view];
    RootVCOfVidio *vidio = [[RootVCOfVidio alloc]init];
    [attention addChildViewController:vidio];
    [self addChildViewController:attention];
    [self.collection addSubview:attention.view];
    [self addChildViewController:expert];
    [self.collection addSubview:expert.view];
    [self addChildViewController:acticity];
    [self.collection addSubview:acticity.view];
    
    
    [vidio release];

    
    [selection.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.collection.mas_top).with.offset(-64);
        make.leading.equalTo(self.collection.mas_top).with.offset(-100);
        make.height.equalTo(self.collection.mas_height).with.offset(0);
        make.width.equalTo(self.collection.mas_width);
    }];
    
    [attention.view mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.equalTo(selection.view.mas_top).with.offset(selection.view.frame.size.width * 1 - 36);
        make.height.equalTo(self.collection.mas_height).with.offset(+ 49);
        make.width.equalTo(selection.view.mas_width);
        
    }];
    [expert.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(selection.view.mas_centerY);
        make.leading.equalTo(attention.view.mas_trailing).with.offset(0);
        make.height.equalTo(selection.view.mas_height);
        make.width.equalTo(attention.view.mas_width);
        
    }];
    [acticity.view mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.equalTo(expert.view.mas_trailing).offset(0);
        make.centerY.equalTo(selection.view.mas_centerY).offset(0);
        make.height.equalTo(selection.view.mas_height);
        make.width.equalTo(expert.view.mas_width);
        
    }];
    
    [selection release];
    [attention release];
    [expert release];
    [acticity release];
    
    
  
    self.titleView = [[UIView alloc]initWithFrame:CGRectMake(0, 63, SCREEN_WIDTH,  SCREEN_HEIGHT - ((self.collection.bounds.size.height) + 112))];
//    self.titleView.backgroundColor = [UIColor purpleColor];
    [self.view addSubview:self.titleView];

#pragma mark - 头部button
    for (int i = 0; i < 4; i++)
    {
        
        NSMutableArray *mArrOfbuttonName = [NSMutableArray array];
        
        [mArrOfbuttonName addObject:@"精选"];
        [mArrOfbuttonName addObject:@"关注"];
        [mArrOfbuttonName addObject:@"达人"];
        [mArrOfbuttonName addObject:@"活动"];
        NSLog(@"%@", mArrOfbuttonName[i]);
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        button.frame = CGRectMake(5 + i * tittle_view_W / 4, 71, tittle_view_W / 4 - 10 , tittle_view_H - 14);
        button.tag = 100 + 1 * i;
        [button setTitleColor:[UIColor colorWithRed:124.0f / 255.0f green:124.0f / 255.0f blue:124.0f / 255.0f alpha:1] forState:UIControlStateNormal];
        [button setTitle: mArrOfbuttonName[i] forState:UIControlStateNormal];
        
        [self.view addSubview:button];
        
    }
    
#pragma mark - 滑动头部红线
    self.viewOfRedLine = [[UIView alloc]initWithFrame:CGRectMake(26, 69 + 29, tittle_view_W / 4 - 50, 1.5)];
    [self.view addSubview:self.viewOfRedLine];
    self.viewOfRedLine.backgroundColor = [UIColor colorWithRed:0.0 / 256.0f  green:208.0 / 256.0f blue:252.0 / 256.0f alpha:1];
    
    
    
    
    [self.viewOfRedLine release];
    
#pragma mark 分割灰线
    UIView *viewOfGrayLine = [[UIView alloc]initWithFrame:CGRectMake(0, 69 + 29 + 2, tittle_view_W, 1)];
    viewOfGrayLine.backgroundColor = [UIColor colorWithRed:124.0f / 255.0f green:124.0f / 255.0f blue:124.0f / 255.0f alpha:0.4];
    
    [self.view addSubview:viewOfGrayLine];
    
    [viewOfGrayLine release];
    [self.titleView release];
    
    
    
    
    
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    NSInteger index = scrollView.contentOffset.x / 5.0f / (CGRectGetWidth(self.view.bounds) / 5);
    
    
    for (UIButton *button in self.titleView.subviews) {
        
        [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    }
    
    [self.titleView.subviews[index] setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
}

#pragma mark - 时间部分设置白色
- (UIStatusBarStyle)preferredStatusBarStyle{
    
    [super preferredStatusBarStyle];
    
    return UIStatusBarStyleLightContent;
    
}



@end
